<template>
	<div>
		<van-nav-bar
		  title="修改密码"
			left-text="返回"
			 @click-left="handleLeftClick"
		/>
		<van-cell-group>
		<van-cell title="账号" :value="userInfo.username" />
			<van-field
				v-model="userInfo.oldPassword" 
				label="原密码" 
				placeholder="请输入原密码" 
				input-align="right"
				type="password"
				clearable
			/>
			<van-field
				v-model="userInfo.password" 
				label="新密码" 
				type="password"
				placeholder="请输入新密码" 
				input-align="right"
				clearable
			/>
			<van-field
				v-model="userInfo.password1" 
				label="确认密码" 
				type="password"
				placeholder="请输入确认密码" 
				input-align="right"
				clearable
			/>
		</van-cell-group>
		<div style="text-align: center;padding: 15px;">
			<van-button type="info" @click="handleSubmit">修改</van-button>
		</div>
	</div>
</template>

<script>
	import {changePass} from '@/api/user-api.js'
	export default{
		data(){
			return {
				userInfo:{}
			}
		},
		methods:{
			handleLeftClick(){
				this.$router.history.go(-1)
			},
			async handleSubmit(){
				if(this.userInfo.password == ''){
					this.$notify({ type: 'danger', message: '密码不可以为空' })
					return
				}
				if(this.userInfo.password1 == ''){
					this.$notify({ type: 'danger', message: '确认密码不可为空' })
					return
				}
				if(this.userInfo.password != this.userInfo.password1){
					this.$notify({ type: 'danger', message: '两次密码不一致' })
					return
				}
				let res = await changePass(this.userInfo)
				
				if(res.data.code == 200){
					this.$notify({ type: 'success', message: '密码修改成功请重新登录' })
					sessionStorage.clear()
					this.$router.push('/login')
				}
			}
		},
		created(){
			this.userInfo = JSON.parse(sessionStorage.userInfo)
			
		}
	}
</script>

<style>
</style>
